package com.example.demo.service.impl;

import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户服务实现类
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User register(User user) {
        userMapper.insert(user);
        return user;
    }

    @Override
    public User login(String username, String password) {
        return userMapper.login(username, password);
    }

//    @Override
//    public User login(String username, String password) {
//        return userMapper.findByUsernameAndPassword(username, password);
//    }

    @Override
    public User updateUser(User user) {
        userMapper.updateById(user);
        return user;
    }

    @Override
    public void deleteUser(Integer userId) {
        userMapper.deleteById(userId);
    }

    @Override
    public User getUser(Integer userId) {
        return userMapper.selectById(userId);
    }

    @Override
    public List<User> getAllUsers() {
        return userMapper.selectList(null);
    }

    @Override
    public List<User> all() {
        return userMapper.all();
    }

    @Override
    public void changePassword(Integer userId, String newPassword) {
        User user = getUser(userId);
        if (user != null) {
            user.setPassword(newPassword);
            updateUser(user);
        }
    }
}